package com.kiln.juxingqs.fragment

import android.content.Intent
import android.view.View
import client.global.commlib.base.BaseActivity
import client.global.commlib.base.BaseFragment
import client.global.commlib.comm_ui.ImageGalleryActivity
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.dialog.NotificationDialog
import client.global.commlib.utils.Util
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.SyAdapter
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.databinding.FragmentTaboneBinding
import com.kiln.juxingqs.ui.WebActivity
import com.kiln.juxingqs.ui.vm.MainViewModel

class TabOneFragment : BaseFragment<FragmentTaboneBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabone
    }

    val adapter: SyAdapter by lazy {
        SyAdapter(requireContext(), mImgLoader)
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    override fun initWidget() {
        binding.event = this
        binding.statusBar.post {
            val h = Util.getStatusBarHeight(requireContext())
            val p = binding.statusBar.layoutParams
            p.height = h
            binding.statusBar.layoutParams = p
//            Util.setBg(resources.getColor(R.color.tranplant), requireActivity().window)
        }
        binding.recyclerView.adapter = adapter
//        val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
//        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
//        decoration.setDrawable(dividerDrawable)
//        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.emptyLayout.setOnClickListener {
            viewModel.homeOnNoti()
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.homeOnNoti()
        }
        viewModel.homeCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    adapter.hasMoreState.value = false
                    val list = it.data!!.goods_list as MutableList<GoodsIndex.Goods?>

                    if (list.size > 0) {
                        adapter.submitData(list, it.data!!)
                        adapter.hasMoreState.value = false
                    }
//                    else {
//                        adapter.submitSuperData(it.data!!)
//                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (adapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
                binding.refreshLayout.isRefreshing = false
            }

        })

        if (viewModel.homeCall.value == null) {
            viewModel.homeOnNoti()
        }
    }

    override fun initData() {
        viewModel.isShowNotiDialog.observe(this, {
            if (it) {
                viewModel.getCommImg()
            }
        })
        viewModel.NotiCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    adapter.notice_list = it.data!!.notice_list
                }
            }
        })
        viewModel.adCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val imgg = it.data!!

                    if (imgg.img_list.size > 0) {

                        NotificationDialog(requireActivity() as BaseActivity<*, *>).apply {
                            commImg = imgg
                            click = {
                                val b = it

                                if (!b.big_img.isNullOrEmpty()) {
                                    ImageGalleryActivity.show(
                                        requireContext(),
                                        b.big_img,
                                        true,
                                        false
                                    )
                                } else if (!b.url.isNullOrEmpty()) {
                                    context.startActivity(
                                        Intent(
                                            context,
                                            WebActivity::class.java
                                        ).apply {
                                            putExtra("adInfo", b.url)
                                            putExtra("title", b.title)
                                        })
                                }
                            }

                            setOnDismissListener {
                                viewModel.isShowNotiDialog.value = false
                            }
                        }.show()
                    } else {
                        viewModel.isShowNotiDialog.value = false
//                        toast("暂无消息")
                    }
                }
            }
        })
    }

    fun getMsg() {
        viewModel.getCommImg()
    }
}